package com.redis;

import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

/**
 * @author lvcaihong
 * @dtae 2019/2/13 18:36
 */
@Service
public class RedisService {
    @Autowired
    JedisPool jedisPool;


    /**
     * Circular reference involving containing bean 'redisService'
     *
     */
    /**
     *  获取对象
     * @param keyPrefix
     * @param key
     * @param clazz
     * @param <T>
     * @return
     */
    public <T> T get(KeyPrefix keyPrefix,String key,Class<T> clazz){
        //注入JedisPool，通过JedisPool获取Jedis
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            //生成真正的key
            String realKey = keyPrefix.getPrefix() + key;
            String value = jedis.get(realKey);
            System.out.println(value);
            //字符串转换为bean对象
            T t = stringToBean(value,clazz);
            return t;
        }finally {
            returnToPool(jedis);
        }
    }

    /**
     *  设置对象
     * @param keyPrefix
     * @param key
     * @param value
     * @param <T>
     * @return
     */
    public <T> boolean set(KeyPrefix keyPrefix,String key,T value){
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            String str = beanToString(value);
            if(str == null || str.length() <=0){
                return  false;
            }
            //生成真正的key
            String realKey = keyPrefix.getPrefix() + key;
            int seconds = keyPrefix.expireSeconds();
            if(seconds <= 0){
                //永远不过期
                jedis.set(realKey,str);
            }else{
                jedis.setex(realKey,seconds,str);
            }
            return true;
        }finally {
            returnToPool(jedis);
        }
    }

    /**
     * 判断key是否存在
     *
     * @param keyPrefix
     * @param key
     * @param <T>
     * @return
     */
    public <T> boolean exit(KeyPrefix keyPrefix,String key){
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            //生成真正的key
            String realKey = keyPrefix.getPrefix() + key;
            //判断key是否存在
            return jedis.exists(realKey);
        }finally {
            returnToPool(jedis);
        }
    }

    /**
     * 增加值
     * @param keyPrefix
     * @param key
     * @param <T>
     * @return
     */
    public <T> Long incr(KeyPrefix keyPrefix,String key){
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            //生成真正的key
            String realKey = keyPrefix.getPrefix() + key;
            //判断key是否存在
            return jedis.incr(realKey);
        }finally {
            returnToPool(jedis);
        }
    }

    /**
     *  减少值
     * @param keyPrefix
     * @param key
     * @param <T>
     * @return
     */
    public <T> Long decr(KeyPrefix keyPrefix,String key){
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            //生成真正的key
            String realKey = keyPrefix.getPrefix() + key;
            //判断key是否存在
            return jedis.decr(realKey);
        }finally {
            returnToPool(jedis);
        }
    }

    private <T> String beanToString(T value) {
        if(value == null){
            return null;
        }
        //获取类对象
        Class<?> clazz = value.getClass();
        if(clazz == int.class || clazz == Integer.class){
            return "" + value;
        }else if(clazz == String.class){
            return (String)value;
        }else if(clazz == long.class || clazz == Long.class){
            return "" + value;
        }else{
            return JSON.toJSONString(value);
        }


    }

    /**
     * 字符串转换为Bean对象
     * @param value
     * @param <T>
     * @return
     */
    private <T> T stringToBean(String value,Class<T> clazz) {
        if(value == null || value.length() <= 0 || clazz == null){
            return null;
        }
        //获取类对象
        if(clazz == int.class || clazz == Integer.class){
            return (T)Integer.valueOf(value);
        }else if(clazz == String.class){
            return (T) value;
        }else if(clazz == long.class || clazz == Long.class){
            return (T) Long.valueOf(value);
        }else{
            return JSON.toJavaObject(JSON.parseObject(value),clazz);
        }
    }

    /**
     * 关闭连接池
     * @param jedis
     */
    private void returnToPool(Jedis jedis) {
        if(jedis != null){
            jedis.close();
        }
    }


}
